home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Buttons and Labels and Scrolls (Oh, My!) / RadioButtons / RadioButtons.cs next >
Encoding:
Text File  |  2001-01-15  |  3.0 KB  |  83 lines

  1. //-------------------------------------------
  2. // RadioButtons.cs ⌐ 2001 by Charles Petzold
  3. //-------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class RadioButtons: Form
  9. {
  10.      bool  bFillEllipse;
  11.      Color colorEllipse;
  12.  
  13.      static void Main()
  14.      {
  15.           Application.Run(new RadioButtons());
  16.      }
  17.      RadioButtons()
  18.      {
  19.           Text = "Radio Buttons Demo";
  20.           ResizeRedraw = true;
  21.  
  22.           string[] astrColor = { "Black", "Blue", "Green", "Cyan",   
  23.                                  "Red", "Magenta", "Yellow", "White"};
  24.  
  25.           GroupBox grpbox = new GroupBox();
  26.           grpbox.Parent   = this;
  27.           grpbox.Text     = "Color";
  28.           grpbox.Location = new Point(Font.Height / 2, Font.Height / 2);
  29.           grpbox.Size     = new Size(9 * Font.Height, 
  30.                               (3 * astrColor.Length + 4) * Font.Height / 2);
  31.  
  32.           for (int i = 0; i < astrColor.Length; i++)
  33.           {
  34.                RadioButton radiobtn = new RadioButton();
  35.                radiobtn.Parent      = grpbox;
  36.                radiobtn.Text        = astrColor[i];
  37.                radiobtn.Location    = new Point(Font.Height,
  38.                                              3 * (i + 1) * Font.Height / 2);
  39.                radiobtn.Size        = new Size(7 * Font.Height, 
  40.                                                3 * Font.Height / 2);
  41.                radiobtn.CheckedChanged += 
  42.                               new EventHandler(RadioButtonOnCheckedChanged);
  43.                if(i == 0)
  44.                     radiobtn.Checked = true;
  45.           }
  46.           CheckBox chkbox = new CheckBox();
  47.           chkbox.Parent   = this;
  48.           chkbox.Text     = "Fill Ellipse";
  49.           chkbox.Location = new Point(Font.Height, 
  50.                               3 * (astrColor.Length + 2) * Font.Height / 2);
  51.           chkbox.Size     = new Size(Font.Height * 7, 3 * Font.Height / 2);
  52.           chkbox.CheckedChanged += 
  53.                          new EventHandler(CheckBoxOnCheckedChanged);
  54.      }
  55.      void RadioButtonOnCheckedChanged(object obj, EventArgs ea)
  56.      {
  57.           RadioButton radiobtn = (RadioButton) obj;
  58.  
  59.           if(radiobtn.Checked)
  60.           {
  61.                colorEllipse = Color.FromName(radiobtn.Text);
  62.                Invalidate(false);
  63.           }
  64.      }
  65.      void CheckBoxOnCheckedChanged(object obj, EventArgs ea)
  66.      {
  67.           bFillEllipse = ((CheckBox)obj).Checked;
  68.           Invalidate(false);
  69.      }
  70.      protected override void OnPaint(PaintEventArgs pea)
  71.      {
  72.           Graphics  grfx = pea.Graphics;
  73.           Rectangle rect = new Rectangle(10 * Font.Height, 0,
  74.                                          ClientSize.Width - 
  75.                                              10 * Font.Height - 1,
  76.                                          ClientSize.Height - 1);
  77.           if(bFillEllipse)
  78.                grfx.FillEllipse(new SolidBrush(colorEllipse), rect);
  79.           else
  80.                grfx.DrawEllipse(new Pen(colorEllipse), rect);
  81.      }
  82. }
  83.